﻿/*
 * Last review by   : Yaser
 * Last review date : JUL-21-09
 */
using System;
using ERP.Business.DataTypes.Constants.Interfaces;
using ERP.Framework.Components.DatabaseHelper;
using ERP.Framework.Components.DatabaseHelper.Providers;

namespace ERP.Business.Company.Policy
{
    /// <summary>
    /// Constants Manager
    /// </summary>
    public abstract class ConstantsManager
    {
        /*
        private const string UPD_INS_PROC_NAME_KEY = "UPD_INS_PROC_NAME";
        private const string SELECT_PROC_NAME_KEY = "SELECT_PROC_NAME";
       
        /// <summary>
        /// Update existing constant value
        /// </summary>
        /// <param name="value">value to update</param>
        /// <returns>update result</returns>
        public static bool UpdateValue(ConstantBase value)
        {
            string commandText = GetResourceEntry(value.GetType(), UPD_INS_PROC_NAME_KEY);
            if (commandText == null || commandText.Equals(string.Empty))
                throw new NullReferenceException();

            GeneralDatabaseProvider con = new GeneralDatabaseProvider(DatabaseConstants.AdministrationDatabase);
            ParametersCollection p = new ParametersCollection();
            p.AddParameter(new Parameter("@ID", value.ID, ParameterType.String));
            p.AddParameter(new Parameter("@Name", value.Name, ParameterType.String));            

            if (value.DirtyFlag == ERP.Business.DataTypes.BaseTypes.DirtyFlag.Delete)
            {
                p.AddParameter(new Parameter("@ObsoleteFlag", "Y", ParameterType.Char));
            }
            else if (value.DirtyFlag == ERP.Business.DataTypes.BaseTypes.DirtyFlag.Insert)
            {
                p.AddParameter(new Parameter("@ObsoleteFlag", "N", ParameterType.Char));
            }
            else
            {
                p.AddParameter(new Parameter("@ObsoleteFlag", value.ObsoleteFlag ? "Y" : "N" , ParameterType.Char));
            }

            int retVal = con.Save(CommandType.StoredProcedure, commandText, p);
            return retVal > 0;
        }

        /// <summary>
        /// Add new constant to DB
        /// </summary>
        /// <param name="value">constant to add</param>
        /// <returns>insertion result</returns>
        public static bool InsertValue(ConstantBase value)
        {
            value.DirtyFlag = ERP.Business.DataTypes.BaseTypes.DirtyFlag.Insert;
            return UpdateValue(value);            
        }

        /// <summary>
        /// Read constant name from DB
        /// </summary>
        /// <param name="constantType">type</param>
        /// <param name="id">constant id</param>
        /// <returns>constant name</returns>
        public static string GetName(Type constantType, string id)
        {
            //Read procedure name
            string commandText = GetResourceEntry(constantType, SELECT_PROC_NAME_KEY);
            if (commandText == null || commandText.Equals(string.Empty))
                throw new NullReferenceException();

            GeneralDatabaseProvider con = new GeneralDatabaseProvider(DatabaseConstants.AdministrationDatabase);
            ParametersCollection p = new ParametersCollection();
            p.AddParameter(new Parameter("@ID", id, ParameterType.String));
            string retVal = con.ReadScalar(CommandType.StoredProcedure, commandText, p).ToString();

            return retVal; 
        }

        /// <summary>
        /// Retrieve all date from DB
        /// </summary>
        /// <param name="constantType">constant type</param>
        /// <returns>collection of constants</returns>
        public static ConstantCollection GetCollection(Type constantType)
        {
            //Read procedure name
            string commandText = GetResourceEntry(constantType, SELECT_PROC_NAME_KEY);
            if (commandText == null || commandText.Equals(string.Empty))
                throw new NullReferenceException();

            GeneralDatabaseProvider con = new GeneralDatabaseProvider(DatabaseConstants.AdministrationDatabase);
            ParametersCollection p = new ParametersCollection();
            p.AddParameter(new Parameter("@ID", DBNull.Value, ParameterType.String));
            System.Data.DataTable table = con.Read(CommandType.StoredProcedure, commandText, p);
            ConstantCollection retVal = null;
            if (table != null && table.Rows.Count > 0)
            {
                retVal = new ConstantCollection();
                foreach (System.Data.DataRow item in table.Rows)
                {
                    ConstantObject obj = new ConstantObject();
                    obj.ID = item["ID"].ToString();
                    obj.Name = item["Name"].ToString();
                    obj.ObsoleteFlag = item["ObsoleteFlag"].ToString().Equals("Y", StringComparison.CurrentCultureIgnoreCase ) ? true : false ;
                    retVal.Add(obj);
                }
            }
            return retVal; 
            
        }
        
        /// <summary>
        /// Excuted delete procedure
        /// </summary>
        /// <param name="value">value to delete</param>
        /// <returns>deletion result</returns>
        public static bool RemoveValue(ConstantBase value)
        {
            value.DirtyFlag = ERP.Business.DataTypes.BaseTypes.DirtyFlag.Delete;
            return UpdateValue(value);
        }

        /// <summary>
        /// Read key from associated resource file
        /// </summary>
        /// <param name="type">Object type</param>
        /// <param name="key">Key name</param>
        /// <returns>Value</returns>
        private static string GetResourceEntry(Type type, string key)
        {
            System.Resources.ResourceManager x = new System.Resources.ResourceManager(type);
            return x.GetString(key);
        }
      * */
    }

    /// <summary>
    /// just to add constant objects in the collection
    /// </summary>
    internal class ConstantObject : ConstantBase
    {
    }
}
